<template>
	<view class="addgroup">
		<u-navbar left-icon-color="#fff" :placeholder="true" :bgColor="bgColor" :autoBack="true">
			<view class="navtitle" slot="center">
				创建群聊
			</view>
		</u-navbar>
		<view class="index-list">
			<view v-if="list.length > 0">
				<checkbox-group @change="checkboxChange">
					<label class="uni-list-cell uni-list-cell-pd" v-for="(item,index) in list" :key="item.value">
						<view class="anchor-list">
							<view class="anchor-left">
								<!-- 使用 u-checkbox 组件作为复选框 -->
								<checkbox :value="JSON.stringify(item.member_id)" color="#17191A"
									:checked="item.show" />
							</view>
							<view class="anchor-right">
								<!-- 在 u-image 中展示用户头像 -->
								<u-image :showLoading="true" radius="10" :src="item.u_img"
									loadingIcon="/static/chat/logo.png" errorIcon="/static/chat/logo.png" width="50px"
									height="50px">
								</u-image>
								<!-- 根据条件显示用户名或备注 -->
								<view class="text" v-if="item.break !== ''">{{ item.break }}</view>
								<view class="text" v-else>{{ item.user }}</view>
							</view>
						</view>
					</label>
				</checkbox-group>
			</view>
			<view v-if="list.length < 1">
				<u-empty mode="list" text="好友列表为空" :marginTop="100" icon="http://cdn.uviewui.com/uview/empty/list.png">
				</u-empty>
			</view>
		</view>
		<view class="flex-btn">
			<u-button type="primary" @click="goadd" style="color: #17191A;"
				color="linear-gradient(90deg, #C8F673 0%, #98F083 100%)" text="创建"></u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				bgColor: 'rgba(0,0,0,0.0)',
				value: '',
				userinfo: {},
				list: [],
				searchlist: [],
				selectedItems: [],
			}
		},
		onLoad() {
			this.userinfo = uni.getStorageSync('info')
			this.userlist()
		},
		methods: {
			goadd() {
				if (this.selectedItems.length < 1) {
					uni.showToast({
						title: '请选择联系人！',
						icon: 'none'
					})
					return
				}
				if (this.selectedItems.length < 3) {
					uni.showToast({
						title: '群聊人数最少3人！',
						icon: 'none'
					})
					return
				}
				uni.showLoading({
					mask: true
				})
				this.$Request({
					method: 'POST',
					url: '/api/rong/addGroups',
					data: {
						use_id: this.userinfo.member_id,
						name: '群聊',
						member_ids: this.selectedItems.toString()
					}
				}).then(res => {
					uni.hideLoading()
					if (res.code == 1) {
						console.log(res)
						uni.showToast({
							title: '创建成功！',
							icon: 'none'
						})
						setTimeout(()=>{
							uni.navigateBack({
								delta:1
							})
						},1000)
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				})
			},
			checkboxChange(e) {
				this.selectedItems = e.detail.value
			},
			gochat(item) {
				let name = item.break
				if (item.break == '') {
					name = item.user
				}
				uni.navigateTo({
					url: '/pages/chat/chitchat/index?id=' + item.member_id + '&name=' + name
				})
			},
			gourl(url) {
				uni.navigateTo({
					url: url
				})
			},
			userlist() {
				this.$Request({
					method: 'POST',
					url: '/api/rong/userFriendsList',
					data: {
						use_id: this.userinfo.member_id
					}
				}).then(res => {
					if (res.code == 1) {
						let list = res.data
						let arr = []
						for (let i in list) {
							list[i].map(item => {
								item.show = false
								arr.push(item)
							})
						}
						this.list = arr
						console.log(this.list)
					}
				})
			},
		}
	}
</script>

<style>
	page {
		background-color: #17191A;
	}
</style>
<style scoped lang="scss">
	.addgroup {
		.flex-btn {
			width: 100%;
			padding: 30rpx;
			position: fixed;
			bottom: 0;
			left: 0;
			background-color: #1D2123;
		}

		.navtitle {
			color: #fff;
		}

		.index-list {
			padding: 30rpx;

			.anchor-list {
				width: 100vw;
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-bottom: 30rpx;

				.anchor-left {
					width: 8%;
				}

				.anchor-right {
					width: 90%;
					display: flex;
					align-items: center;

					.text {
						color: #fff;
						font-size: 28rpx;
						margin-left: 20rpx;
					}
				}
			}
		}
	}
</style>